package com.boot.web;

import com.boot.entity.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * @author Chunsheng.Zhang
 * @date 2021/1/8 10:42
 * @description : TODO
 */
@RestController
@RequestMapping("/file")
public class FileController {


    /**
     *
     *
     * 单文件上传
     *
     *
     * */
    @RequestMapping("/uploadSingleFile")
    public String uploadSingleFile(MultipartFile singleFile){
        String uploadFolder = "C:\\Users\\Administrator\\Desktop\\imgs\\single\\";

        File uploadDir = new File(uploadFolder);
        if(!uploadDir.exists() || !uploadDir.isDirectory()) {
            uploadDir.mkdirs();
        }

        try {
            String originalFilename = singleFile.getOriginalFilename();
            File target = new File(uploadFolder+ originalFilename);
            singleFile.transferTo(target);
        } catch (Exception e) {
            e.printStackTrace();
            return "上传失败!";
        }

        return "上传成功！";
    }




    /**
     *
     *
     * 多文件上传
     *
     *
     * */
    @RequestMapping("/uploadMultiFile")
    public String uploadSingleFile(MultipartFile[] files){

        String uploadFolder = "C:\\Users\\Administrator\\Desktop\\imgs\\multi\\";

        File uploadDir = new File(uploadFolder);
        if(!uploadDir.exists() || !uploadDir.isDirectory()) {
            uploadDir.mkdirs();
        }


        for (MultipartFile file : files) {
            try {
                String originalFilename = file.getOriginalFilename();
                File target = new File(uploadFolder+ originalFilename);
                file.transferTo(target);
            } catch (IOException e) {
                e.printStackTrace();
                return "上传失败!";
            }
        }
        return "多文件上传成功！";
    }



    /**
     *
     * 结合表单实现文件上传
     *
     * */
    @RequestMapping("/addUser")
    public User addUser(User user){


        return null;
    }








}
